package com.hospital;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hospital.pony.adapter.ReplyAdapter;
import com.hospital.pony.adapter.TopicImgAdapter;
import com.hospital.pony.base.BaseActivity;
import com.hospital.pony.config.Consts;
import com.hospital.pony.db.MemberUserUtils;
import com.hospital.pony.model.ResponseEntry;
import com.hospital.pony.model.ReviewModel;
import com.hospital.pony.model.TopicModel;
import com.hospital.pony.util.ToastUtil;
import com.hospital.pony.view.GridviewForScrollView;
import com.hospital.pony.view.ListviewForScrollView;

import net.tsz.afinal.http.AjaxParams;

import java.util.List;


public class ReviewShareMessageActivity extends BaseActivity {
    // title
    private TextView mTvTitle, mIvStu;
    // 返回
    private ImageView mIvBack;
    // 查询按钮
    private TopicModel noticeModel;

    private TextView mtvtime;

    private RelativeLayout mllbottom;

    private TextView topicInfor;
    private EditText replyMessage;
    private LinearLayout mllimg;
    private Button mbtnSend;
    private TextView notipifnor;
    private ListviewForScrollView mListReviewMessage;

    private TextView mtvSendName;
    private GridviewForScrollView gvImgShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_review);
        initWidget();
        initData();

    }



    @Override
    public void initWidget() {

        gvImgShow = (GridviewForScrollView) findViewById(R.id.gvImgShow);
        mtvSendName = (TextView) findViewById(R.id.mtvSendName);

        mListReviewMessage = (ListviewForScrollView) findViewById(R.id.mListReviewMessage);
        notipifnor = (TextView) findViewById(R.id.notipifnor);
        mbtnSend = (Button) findViewById(R.id.mbtnSend);
        mbtnSend.setOnClickListener(this);
        mllimg = (LinearLayout) findViewById(R.id.mllimg);
        replyMessage = (EditText) findViewById(R.id.replyMessage);
        topicInfor = (TextView) findViewById(R.id.topicInfor);
        mllbottom = (RelativeLayout) findViewById(R.id.mllbottom);
        mtvtime = (TextView) findViewById(R.id.mtvtime);

        mIvBack = (ImageView) findViewById(R.id.mIvBack);
        mTvTitle = (TextView) findViewById(R.id.mTvTitle);
        mTvTitle.setText("信息详情");
        mIvBack.setVisibility(View.VISIBLE);
        mIvBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.mIvBack){
            ReviewShareMessageActivity.this.finish();
        }
        if(v.getId()==R.id.mbtnSend){
            if (TextUtils.isEmpty(replyMessage.getText().toString())) {
                ToastUtil.ShowCentre(this, "请输入信息");
                return;
            }
            addReviewMessage(true);
        }
    }



    @Override
    public void initData() {

        noticeModel = (TopicModel) this.getIntent().getSerializableExtra("msg");


        TopicImgAdapter topicGridAdminAdapter = new TopicImgAdapter(this, noticeModel.getTopicImg().split(","));
        gvImgShow.setAdapter(topicGridAdminAdapter);


        mtvSendName.setText(noticeModel.getTopicUserName());
        topicInfor.setText(noticeModel.getTopicInfor());
        mtvtime.setText(noticeModel.getTopicTime());


        replyMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().length() > 0) {
                    mllimg.setVisibility(View.GONE);
                    mbtnSend.setVisibility(View.VISIBLE);

                } else {
                    mllimg.setVisibility(View.VISIBLE);
                    mbtnSend.setVisibility(View.GONE);
                }
            }
        });
        ListNoticesMessage(false);
    }



    private void addReviewMessage(boolean isShow) {
        AjaxParams params = new AjaxParams();
        params.put("action_flag", "addReview");
        params.put("reviewUserId", MemberUserUtils.getUid(this));
        params.put("reviewUserName", MemberUserUtils.getName(this));
        params.put("reviewMessageId", noticeModel.getTopicId() + "");
        params.put("reviewContent", replyMessage.getText().toString() + "");
        httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultCode, isShow, "正在提交...");
    }

    private void ListNoticesMessage(boolean isShow) {
        AjaxParams params = new AjaxParams();
        params.put("action_flag", "reviewListMessage");
        params.put("reviewMessageId", noticeModel.getTopicId() + "");
        httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultFlag, isShow, "正在提交...");
    }

    List<ReviewModel> list_result;
    ReplyAdapter replyAdapter;

    @Override
    protected void callBackSuccess(ResponseEntry entry, int actionId) {
        super.callBackSuccess(entry, actionId);
        switch (actionId) {

            case Consts.actionId.resultCode:
                ListNoticesMessage(false);
                break;
            case Consts.actionId.resultFlag:

                if (null != entry.getData() && !TextUtils.isEmpty(entry.getData())) {

                    String jsonMsg = entry.getData().substring(1, entry.getData().length() - 1);
                    if (null != jsonMsg && !TextUtils.isEmpty(jsonMsg)) {
                        list_result = mGson.fromJson(entry.getData(), new TypeToken<List<ReviewModel>>() {
                        }.getType());
                        replyAdapter = new ReplyAdapter(this, list_result);
                        mListReviewMessage.setAdapter(replyAdapter);
                        notipifnor.setVisibility(View.GONE);
                    } else {
                        notipifnor.setVisibility(View.VISIBLE);
                    }
                }

                break;
        }


    }

    @Override
    protected void callBackAllFailure(String strMsg, int actionId) {
        super.callBackAllFailure(strMsg, actionId);
    }



}
